import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({
  showSpinner: false
})

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL), // 指定路由模式
  routes: [
    {
      path: '/login',
      // vite规定，我们路径中如果是.vue结尾，要加上
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: '/article',
          component: () => import('@/views/article/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        {
          path: '/notify',
          component: () => import('@/views/notify/index.vue'),
          meta: {
            title: '消息中心'
          }
        },
        {
          path: '/user',
          component: () => import('@/views/user/index.vue'),
          meta: {
            title: '个人中心'
          }
        }
      ]
    },
    {
      path: '/login',
      // vite规定，我们路径中如果是.vue结尾，要加上
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/user/patient',
      component: () => import('@/views/user/patient-page.vue'),
      meta: {
        title: '家庭档案'
      }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/consult/consult-fast.vue'),
      meta: {
        title: '急速问诊'
      }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/consult/consult-dep.vue'),
      meta: {
        title: '选择科室'
      }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/consult/consult-illness.vue'),
      meta: {
        title: '图文问诊'
      }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/consult/consult-pay.vue'),
      meta: {
        title: '支付'
      }
    },
    {
      path: '/room',
      component: () => import('@/views/room/index.vue'),
      meta: {
        title: '医生问诊室'
      },
      // 局部导航守卫
      beforeEnter (to) {
        if (to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/user/consult-page.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/user/consult-detail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/order-logistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/login-callback.vue'),
      meta: { title: 'QQ登录-手机号绑定' }
    },
    // 测试
    {
      path: '/test',
      component: () => import('@/views/test/index.vue'),
      meta: {
        title: '家庭档案'
      }
    },
    {
      path: '/map',
      component: () => import('@/views/test/map.vue'),
      meta: {
        title: '地图'
      }
    },
    {
      path: '/mock',
      component: () => import('@/views/test/mock.vue'),
      meta: {
        title: '地图'
      }
    }
  ]
})

// 不需要登录的页面，白名单
const whiteList = ['/login', '/register', '/login/callback']
router.beforeEach(to => {
  NProgress.start()
  const store = useUserStore()

  // 如果没有登录且不在白名单内，去登录
  if (!store.user?.token && !whiteList.includes(to.path))
    return `/login?redirect=${to.fullPath}`
  // 否则不做任何处理
})

router.afterEach(to => {
  NProgress.done()
  // 设置标题
  document.title = `优医问诊-${to.meta.title}`
})

export default router
